import router from "@/router";

export default {
  state: {
    taglist: [{ name: "首页", path: "/Home/Mainpage",bool:true},],
  },
  getters: {
    taglist(state) {
      return state.taglist;
    },
  },
  mutations: {
    //追加标签
    addlist({ taglist }, item) {
      console.log(item,9888);
      if(item.path=="/Login"){
        return
      } 
      console.log(item);
      let toto = true;
      for (const i of taglist) {
        if (item.meta.title === i["name"]) {
          i['query'] = item.query
          toto = false;
        }
      }
      // 如果不为真就不进入，否则就追加数据
      if (toto) {
        taglist.push({
          name: item.meta.title,
          path: item.path,
          query:item.query
        });
      }
    },
    //删除
    handleClose(state, index) {
      
      let taglist = state.taglist
      //   删除选中的樣式
      taglist.splice(index, 1);
      if (index == taglist.length) {
        // 判断当前路由路径为当前选中标签的路径时 并且 当前的下标等于标签的当前长度时
        //  让这个标签的路由跳到上一个标签
        router.push({ path: taglist[index - 1].path });
      } else {
        // 让这个标签的路由跳到下一个标签
        router.push({ path: taglist[index].path });

      }
    },
    //点击标签跳转
    arrs({taglist}, path) {
      console.log(taglist);
      let tea = taglist.find(item=>item.path==path)
     router.push( { path, query:tea.query});
    },
      
    
    //全部刪除
    CloseArr(state) {
      let taglist = state.taglist
      router.push("/Home/Mainpage");
      taglist.splice(1, taglist.length + 1);
    },
    //右侧关闭
    kalos({taglist},index) {
      taglist[index].bool = true
      router.push(taglist[index].path)
      if(taglist.length-1>index){
        let num = taglist.length-1 - index
        taglist.splice(index+1,num)
      }
    },
  },
  actions: {
    addlist(context,index) {
      context.commit("addlist", index);
    },
    handleClose(context, index) {
      if(typeof(index)!="number"){
        // console.log(index);
      var v = context.state.taglist.findIndex(item=>item.path==index)
      context.commit("handleClose", v);return
        
      }
      context.commit("handleClose", index);
    },
    arrs(context, index) {
      context.commit("arrs", index);
    },
    CloseArr(context, index) {
      context.commit("CloseArr", index);
    },
    kalos(context, index) {
      context.commit("kalos", index);
    },
  },
};
